package com.lzb.newloan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.HttpUtils;
import com.lzb.newloan.utils.AlertDialog;
import org.json.JSONException;
import org.json.JSONObject;

public class Register2Activity extends BaseActivity implements OnClickListener {
	private EditText  pwd,  adv_phone;

	private CheckBox check;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register2);
		initView();
	}

	private void initView() {

		pwd = (EditText) findViewById(R.id.pwd);

		adv_phone = (EditText) findViewById(R.id.adv_phone);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("注册");
		Button register_btn = (Button) findViewById(R.id.register_btn);
		register_btn.setOnClickListener(this);

		TextView problem = (TextView) findViewById(R.id.problem);
		problem.setOnClickListener(this);
		RelativeLayout backpress = (RelativeLayout) findViewById(R.id.backpress);
		backpress.setOnClickListener(this);

		check = (CheckBox) findViewById(R.id.check);
		findViewById(R.id.img_eyes).setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {

		case R.id.img_eyes:
			 int isVisible = pwd.getInputType();
			    int cursorPosition = pwd.length();
			    if (isVisible == (InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD)) {
//			        mPwdIsVisible.setImageResource(R.drawable.password_visible);
			    	pwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			    	pwd.setSelection(cursorPosition);
			    }else{
//			        mPwdIsVisible.setImageResource(R.drawable.password_unvisible);
			    	pwd.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD);
			    	pwd.setSelection(cursorPosition);
			    }
			break;

		case R.id.register_btn:
			if (!check.isChecked()) {
				new AlertDialog(Register2Activity.this).builder().setMsg("请先勾选上协议")
				.setNegativeButton("确定", new OnClickListener() {
					@Override
					public void onClick(View v) {

					}
				}).show();
				return;
			}
			register();
			break;
		case R.id.problem:
			Intent intent = new Intent(Register2Activity.this, ProblemActivity.class);
			intent.putExtra("title", "注册协议");
			intent.putExtra("url", Config.REGISTER_PROTOCOL);
			startActivity(intent);
			break;
		case R.id.backpress:
			finish();
			break;
		default:
			break;
		}
	}

	private void register() {

		String pwdStr = pwd.getText().toString();
		String refferee = adv_phone.getText().toString();
		String url = Config.REGISTER + "&mobile=" + getIntent().getStringExtra("phone")
				+ "&u_ip=" + getIntent().getStringExtra("code") + "&pwd=" + pwdStr + "&refferee="
				+ refferee ;
		HttpUtils.doPostAsyn(url,"", mHandler, Config.CODE_RGISTER);
	}

	private Handler mHandler = new Handler() {

		public void handleMessage(Message msg) {
			switch (msg.what) {

			case Config.CODE_URL_ERROR:
				Toast.makeText(Register2Activity.this, "url错误",
						Toast.LENGTH_SHORT).show();
				break;
			case Config.CODE_NET_ERROR:
				Toast.makeText(Register2Activity.this, "网络错误",
						Toast.LENGTH_SHORT).show();
				break;

			case Config.CODE_RGISTER:
				String result = msg.obj.toString();
				try {
					JSONObject json = new JSONObject(result);
					int error = json.getInt("error");
					if (error > 0) {
						Toast.makeText(Register2Activity.this,
								json.getString("msg"), Toast.LENGTH_SHORT)
								.show();
					}else {
						Toast.makeText(Register2Activity.this,
								"注册成功", Toast.LENGTH_SHORT)
								.show();
						SharedPreferences sp =	getSharedPreferences("config",MODE_PRIVATE);
						sp.edit().putString("phone","").commit();
						startActivity(new Intent(Register2Activity.this, LoginActivity.class));
						finish();
					}
				} catch (JSONException e) {
					Toast.makeText(Register2Activity.this, "数据解析错误",
							Toast.LENGTH_SHORT).show();
					e.printStackTrace();
				}

				break;

			default:
				break;
			}
		};
	};


	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub
		
	}

}
